import pickle
import pandas as pd
# 文件和数据处理类
class pkl:
    # pkl读写类
    def __init__(self):
        self.name='文件读写及处理类'

    def read_pkl(self,file_path):
        with open(file_path, "rb") as f:
            data = pickle.load(f)
        return data
    def write_pkl(self,data_dict):
        f = open('./data.pkl', 'wb')
        pickle.dump(data_dict, f, -1)

    # 将{日期：一天内股票数据}转换为{股票：所有时间的数据}
    def reverse(self,info):
        data = {}
        namelist = info["2010-01-04"].index.tolist()
        timelist = info.keys()
        columnlist = info["2010-01-04"].columns.tolist()
        for name in namelist:
            data[name] = pd.DataFrame(columns=columnlist)
        for date in timelist:
            for name in namelist:
                data[name].loc[date] = info[date].loc[name]

        # for name in namelist:
        #     print(data[name])
        return data

    # 将助教股票代码'XXXX.XSHG'转为'sh.XXXX'
    def code_reverse(self,code_list):
        list = []
        for code in code_list:
            l = code.split('.')
            if l[1] == 'XSHG':
                code = 'sh.' + l[0]
            else:
                code = 'sz.' + l[0]
            list.append(code)
        return list

    # 将股票代码'sh.XXXX'转为'XXXX.SH'
    def code_rev(self,code):
        l=code.split('.')
        if l[0] == 'sh':
            code = l[1] + '.SH'
        else:
            code = l[1] + '.SZ'
        return code


    # 将{日期：一天内股票数据}转换为{股票：所有时间的数据}
    def reverse(self,info):
        data = {}
        namelist = info["2010-01-04"].index.tolist()
        timelist = info.keys()
        columnlist = info["2010-01-04"].columns.tolist()
        for name in namelist:
            data[name] = pd.DataFrame(columns=columnlist)
        for date in timelist:
            for name in namelist:
                data[name].loc[date] = info[date].loc[name]
        # for name in namelist:
        #     print(data[name])
        return data



